package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-19
 * Time: 17:30
 */
public class ReflectClassDemo {
    //创建Student类的对象
    public static void reflectNewInstance() {
        Class<?> stu;
        //获取类的对象
        try {
            stu = Class.forName("reflectdemo.Student");
            Student student = (Student)stu.newInstance();
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射构造方法
    public static void reflectPrivateConstructor() {
        Class<?> stu;
        try {
            //获取类的对象
            stu = Class.forName("reflectdemo.Student");
            //获取类的构造方法
            Constructor<?> constructor = stu.getDeclaredConstructor(String.class,int.class);
            //私有可以访问
            constructor.setAccessible(true);
            //修改构造方法
            Student student = (Student)constructor.newInstance("张三",18);
            System.out.println(student);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    //反射私有属性
    public static void reflectPrivateField() {
        Class<?> stu;
        try {
            stu = Class.forName("reflectdemo.Student");

            Field field = stu.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student)stu.newInstance();
            field.set(student,"张三");

            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有方法
    public static void reflectPrivateMethod() {
        Class<?> stu;
        //获取类对象
        try {
            stu = Class.forName("reflectdemo.Student");
            //获取类的私有方法
            Method method = stu.getDeclaredMethod("function",String.class);
            //访问私有
            method.setAccessible(true);
            //创建对象
            Student student = (Student)stu.newInstance();
            //修改方法参数
            method.invoke(student,"你好呀");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //创建class对象
        reflectNewInstance();
        //获取构造方法
        //reflectPrivateConstructor();
        //获取私有属性
        //reflectPrivateField();
        //获取私有方法
        //reflectPrivateMethod();
    }

}
